﻿Imports System.IO
Imports System.Management
Imports System.Text
Imports Gameloop.Vdf

Module Helper


    Function GetSteamIDFromFolder(folderPath As String) As Integer
        Dim steamID As Integer
        Dim steamNameAndID As SteamNameAndID

        Dim value As (appID As Integer, gameName As String, installDir As String) = GetSteamNameAndIDFromFolder(folderPath)
        steamNameAndID = value

        steamID = steamNameAndID.appID

        Return steamID
    End Function

    Function GetSteamNameAndIDFromFolder(path As String) As (appID As Integer, gameName As String, installDir As String)

        ' 创建一个表示工作目录的DirectoryInfo对象  
        Dim workingDir As New IO.DirectoryInfo(path)

        ' 获取工作目录的父目录，并赋值给parentfolder  
        Dim parentfolder As New IO.DirectoryInfo(workingDir.Parent.FullName)

        ' 检查parentfolder的名称是否为"steamapps"，如果不是则返回Nothing  
        If Not parentfolder?.Name = "steamapps" Then
            Return Nothing
        End If

        ' 遍历parentfolder目录下所有的.acf文件  
        For Each fl In parentfolder.EnumerateFiles("*.acf").Where(Function(f) f.Length > 0)
            ' 读取并反序列化.acf文件的内容，得到一个VConv对象  
            Dim vConv = VdfConvert.Deserialize(File.ReadAllText(fl.FullName))

            ' 检查vConv中的"installdir"值是否与工作目录的名称相同  
            If vConv.Value.Item("installdir").ToString = workingDir.Name Then
                ' 如果相同，则从vConv中获取"appid"、"name"和"installdir"的值，并返回结果  
                Dim appID As Integer = CInt(vConv.Value.Item("appid").ToString)
                Dim sName As String = vConv.Value.Item("name").ToString
                Dim sInstallDir As String = vConv.Value.Item("installdir").ToString
                Return (appID, sName, sInstallDir)
            End If
        Next

        ' 如果遍历完所有.acf文件都没有找到匹配的，则返回Nothing  
        Return Nothing
    End Function

    Function getUID() As String

        Dim MacID As String = String.Empty
        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        For Each mo As ManagementObject In moc
            If (MacID = String.Empty And CBool(mo.Properties("IPEnabled").Value) = True) Then
                MacID = mo.Properties("MacAddress").Value.ToString()
            End If
        Next
        Return Convert.ToBase64String(Encoding.UTF8.GetBytes(MacID))

    End Function


End Module

Friend Class SteamNameAndID
    Friend Function appID() As Integer
        Throw New NotImplementedException()
    End Function

    Public Shared Widening Operator CType(v As (appID As Integer, gameName As String, installDir As String)) As SteamNameAndID
        Throw New NotImplementedException()
    End Operator
End Class
